package top.hmtools.jsManager;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import top.hmtools.configurationManager.JsCssProperties;

/**
 * javascript相关操作的controller
 * @author HyboJ
 * 创建日期：2017-9-26下午10:33:07
 */
public class JavascriptController {
	
	@Autowired
	private IJavascriptManager javascriptManager;
	
	@Autowired
	private JsCssProperties jsCssProperties;

	/**
	 * 获取合并的javascript文件内容
	 * @param request
	 * @param response
	 */
	public void getJS(HttpServletRequest request,HttpServletResponse response){
		PrintWriter writer = null;
		try {
			String jsNames = request.getParameter("jsNames");
			String jsContent = this.javascriptManager.getJs(jsNames);
			
			response.reset();
			response.setBufferSize(2048);
			response.setCharacterEncoding(this.jsCssProperties.getEncoding());
//			response.setContentLength(jsContent.length());
			response.setContentType("application/javascript");
			response.setStatus(HttpServletResponse.SC_OK);
			writer = response.getWriter();
			writer.print(jsContent);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e.getMessage());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}finally{
			if(writer != null){
				writer.close();
			}
		}
	}
	
	/**
	 * 刷新javascript缓存库
	 * @param request
	 * @param response
	 */
	public void refreshJS(HttpServletRequest request,HttpServletResponse response){
		PrintWriter writer = null;
		try {
			boolean refresh = this.javascriptManager.refresh();
			
			response.reset();
			writer = response.getWriter();
			writer.println("refresh javascript repertory status is "+refresh);
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e.getMessage());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}finally{
			if(writer != null){
				writer.close();
			}
		}
	}
	
	/**
	 * 获取所有javascript缓存库中js文件名
	 * @param request
	 * @param response
	 */
	public void listJsMenu(HttpServletRequest request,HttpServletResponse response){
		PrintWriter writer = null;
		try {
			List<String> listJsFilenames = this.javascriptManager.listJsFilenames();
			
			response.reset();
			response.setBufferSize(2048);
			response.setCharacterEncoding(this.jsCssProperties.getEncoding());
//			response.setContentLength(jsContent.length());
			response.setContentType("text/html");
			response.setStatus(HttpServletResponse.SC_OK);
			writer = response.getWriter();
			writer.println("<h4>All javascript file name is:</h4>");
			writer.println("<a href=\""+this.jsCssProperties.getRefresh_js_uri()+"\"><h4>Refresh js store</h4></a>");
			for(String jsTmp:listJsFilenames){
				writer.println("<a href=\""+this.jsCssProperties.getJs_uri()+"?jsNames="+jsTmp+"\">"+jsTmp+"</a><br>");
				writer.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,e.getMessage());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}finally{
			if(writer != null){
				writer.close();
			}
		}
	}
}
